﻿using System;

using Spencen.Mobile.Markup.Converters;

namespace Spencen.Mobile.UI.Markup.Converters
{
    public class SizeConverter : Converter<DesiredSize>
    {
        #region IConverter<DesiredSize> Members

        public override DesiredSize ConvertFromString( string input, Type toType )
        {
            if ( input.Equals( "Unbound", StringComparison.OrdinalIgnoreCase ) )
                return DesiredSize.Unbound;
            else
            {
                var axis = input.Split( ',' );
                if ( axis.Length == 2 )
                {
                    var result = new DesiredSize();
                    if ( axis[ 0 ].Equals( "UnboundAxis", StringComparison.OrdinalIgnoreCase ) )
                        result.Width = DesiredSize.UnboundAxis;
                    else
                        result.Width = int.Parse( axis[ 0 ] );
                    if ( axis[ 1 ].Equals( "UnboundAxis", StringComparison.OrdinalIgnoreCase ) )
                        result.Height = DesiredSize.UnboundAxis;
                    else
                        result.Height = int.Parse( axis[ 1 ] );
                    return result;
                }
            }
            throw new ArgumentOutOfRangeException( "input " );
        }

        public override string ConvertToString( DesiredSize input )
        {
            if ( input.IsUnbound )
                return "Unbound";
            else
                return string.Format( "{0},{1}",
                    input.IsXUnbound ? "UnboundAxis" : input.Width.ToString(),
                    input.IsYUnbound ? "UnboundAxis" : input.Height.ToString() );
        }

        #endregion
    }
}
